﻿#region using

using System;
using System.Collections.Generic;
using System.IO;
using MsBuildAccess.Common;

#endregion

namespace MsBuildAccess.Helper
{
    /// <summary>
    /// Factory class
    /// </summary>
    public static class Factory
    {
        /// <summary>
        /// Open text file and create accessrefernces
        /// </summary>
        /// <param name="fileName">Access reference file name</param>
        /// <param name="errorMessage">Error message if any</param>
        /// <returns>List of all msaccess references</returns>
        public static List<AccessReference> CreateAccessReferencesFromFile(string fileName, out string errorMessage)
        {
            errorMessage = string.Empty;
            var referenceses = new List<AccessReference>();
            if (File.Exists(fileName))
            {
                try
                {
                    string[] refTxt = File.ReadAllLines(fileName);
                    if (refTxt != null)
                    {
                        foreach (var s in refTxt)
                        {
                            string[] values = s.Split(',');
                            referenceses.Add(new AccessReference
                                                 {
                                                     Name = values[0],
                                                     Guid = values[1],
                                                     Major = Convert.ToInt32(values[2]),
                                                     Minor = Convert.ToInt32(values[3])
                                                 });
                        }
                    }
                }
                catch (Exception ex)
                {
                    errorMessage = string.Format("Wrong references file format error: {0}", ex);
                }
            }
            return referenceses;
        }

        /// <summary>
        /// Get excluded file types
        /// </summary>
        /// <param name="excludedFileTypes">excluded file types comma separated list default:"txt,acn,acb,scc"</param>
        /// <returns>returns a list with excluded file types</returns>
        public static List<string> GetExcludedList(string excludedFileTypes)
        {
            return new List<string>(string.IsNullOrEmpty(excludedFileTypes)
                                        ? "txt,acn,acb,scc".Split(',')
                                        : excludedFileTypes.Split(','));
        }

        /// <summary>
        /// Get files from directory
        /// </summary>
        /// <param name="directory">directory of the source controlled microsoft access files</param>
        /// <param name="pattern">pattern for microsoft access files (*.a*)</param>
        /// <returns>returns a file queue of the source controlled microsoft access files</returns>
        public static Queue<string> GetFiles(string directory, string pattern)
        {
            return new Queue<string>(Directory.GetFiles(directory, pattern));
        }

        // <summary>
        /// Get List<MsAccessFile></MsAccessFile> as definied in count
        /// </summary>
        /// <param name="fileName">microsoft acces file name</param>
        /// <param name="accessType">access type mdb,accdb,adp</param>
        /// <param name="count">how many instacr</param>
        /// <returns>returns instances of MsAccessFiles</returns>
        public static List<MsAccessFile> GetMsAccesList(string fileName, AccessType accessType, int count,string compileModuleName,Logger logger)
        {
            var accfiles = new List<MsAccessFile>();
            for (int i = 0; i < count; i++)
            {
                MsAccessFile item = new MsAccessFile(fileName, accessType, compileModuleName, logger);
                item.Open();
                accfiles.Add(item);
            }

            return accfiles;
        }
    }
}